<?php
include_once ("../Login/databaseconnection.php");
$Competition_Name=$_POST['Competition_Name'];
 $resultnet=mysql_query("SELECT * FROM competitions WHERE Competition_Name='$Competition_Name'");
  if($rownet=mysql_fetch_array($resultnet))
  {
	$Competition_Id=$rownet['Competition_Id'];
}
$SchName=$_POST['SchName'];
$name=explode(",",$SchName);
$nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $School_Id=$row['School_Id'];
}
	  $Date=$_POST['Date'];
   if( $Competition_Name!="Select One"&&$SchName!=""&&$Date!="Select One")
	   {
	   $result=mysql_query("SELECT * FROM competition WHERE Competition_Id='$Competition_Id' AND School_Id='$School_Id' AND Date='$Date'");
	    if(mysql_num_rows($result)==0)
   {

	   $_SESSION['errornorow']="Very sorry there is no result,Please check your input and try again!";
   }
   else
   {
   if($_SESSION['Access_Type']=="Administrator")
   {
   if($row=mysql_fetch_array($result))
   {
    $Competition_Id=$row['Competition_Id'];
   $resultco=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$Competition_Id'");
  if($rowco=mysql_fetch_array($resultco))
  {
		$Competition_Name=$rowco['Competition_Name'];
	}
	  $School_Id=$row['School_Id'];
   $No_Of_Students=$row['No_Of_Students'];
   $No_Of_Teachers=$row['No_Of_Teachers'];
   $Result=$row['Result'];
   $Date=$row['Date'];
   $Scope=$row['Scope'];
   $Winner=$row['Winner']; 
$Last_Updated_Date=$row['Last_Updated_Date'];   
   }
   date_default_timezone_set("Europe/Paris");
    $t_d=date("Y/m/d");
   	$del=mysql_query("DELETE FROM competition WHERE Competition_Id='$Competition_Id' AND School_Id='$School_Id' AND Date='$Date'");
	if(mysql_affected_rows()!=0)
  {
$_SESSION['error']="This record is successfully deleted!"; 
  }
  if(mysql_affected_rows()==0)
  {
 $_SESSION['error']="Sorry unable to delete this record!";
 $Competition_Name='';
    $Competition_Id='';
   $School_Id='';
   $No_Of_Students='';
   $No_Of_Teachers='';
   $Result='';
   $Date='';
   $Scope='';
   $Winner=''; 
$Last_Updated_Date='';  
   }
  }  
  else if($_SESSION['Access_Type']=="Teacher in Charge")
   {
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
  
   if($School_Id==$Own_School_Id)
	{
	date_default_timezone_set("Europe/Paris");
    $t_d=date("Y/m/d");
	
	$Accessed_By=$_SESSION['NIC_Number'];
	if($row=mysql_fetch_array($result))
   {
   $Competition_Id=$row['Competition_Id'];
   $resultco=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$Competition_Id'");
  if($rowco=mysql_fetch_array($resultco))
  {
		$Competition_Name=$rowco['Competition_Name'];
	}
     $School_Id=$row['School_Id'];
   $No_Of_Students=$row['No_Of_Students'];
   $No_Of_Teachers=$row['No_Of_Teachers'];
   $Result=$row['Result'];
   $Date=$row['Date'];
   $Scope=$row['Scope'];
   $Winner=$row['Winner']; 
$Last_Updated_Date=$row['Last_Updated_Date'];   
   }
   	 $sql=mysql_query("INSERT INTO deleted_competition(Competition_Id,School_Id,No_Of_Students,No_Of_Teachers,Result,Date,Scope,Winner,Deleted_date,Accessed_By,Last_Updated_Date)values('$Competition_Id','$School_Id','$No_Of_Students','$No_Of_Teachers','$Result','$Date','$Scope','$Winner','$t_d','$Accessed_By','$Last_Updated_Date')");
if($sql){
$del=mysql_query("DELETE FROM competition WHERE Competition_Id='$Competition_Id' AND School_Id='$School_Id' AND Date='$Date'");
	if(mysql_affected_rows()!=0)
  {
$_SESSION['error']="This record is successfully deleted!";
 
  }
  if(mysql_affected_rows()==0)
  {
 $_SESSION['error']="Sorry unable to delete this record!";
 mysql_query("DELETE FROM deleted_competition WHERE Competition_Id='$Competition_Id' AND Date='$Date' AND School_Id='$School_Id' AND Deleted_Date='$t_d'");
  $Competition_Name='';
   
  $Competition_Id='';
   $School_Id='';
   $No_Of_Students='';
   $No_Of_Teachers='';
   $Result='';
   $Date='';
   $Scope='';
   $Winner=''; 
$Last_Updated_Date='';  
  }
  }
     if(!$sql)
{
 $_SESSION['error']="Sorry unable to delete this record!";
 if($row=mysql_fetch_array($result))
   {
	$Competition_Name='';
	$Competition_Id='';
   $School_Id='';
   $No_Of_Students='';
   $No_Of_Teachers='';
   $Result='';
   $Date='';
   $Scope='';
   $Winner=''; 
$Last_Updated_Date='';   
   }
 }
  }
    else
  {
  $_SESSION['errornot']="Access denied,Sorry you are not allowed!";
  }
  }
  }
  else
  {
  $_SESSION['errornot2']="Sorry you are not an authorised user!";
  }
  }
  }
   if( $Competition_Id=="Select One"||$SchName==""||$Date=="Select One")
	   {
	   $_SESSION['errorin']="Please select the Competition Name,School's Name and Date!";
		 }
  ?>
